﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake.Net
{
    class HttpRequest
    {
        public static string url = "http://45.251.242.42:8080/";
        public static string GetBarrierJson(int id)
        {
            string strURL = string.Format("{0}?snake={1}", url, "getbarrier");
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(id.ToString());
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            using (StreamReader myreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                string responseText = myreader.ReadToEnd();
                myreader.Close();
                return responseText;
            }
        }
        public static int GetBarrierCount()
        {
            string strURL = string.Format("{0}?snake={1}", url, "getcounts");
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string scount = reader.ReadToEnd();
                reader.Close();
                int count = int.Parse(scount);
                return count;
            }
        }
        public static string Regist(string username,string password)
        {
            string strURL = string.Format("{0}?snake={1}", url, "regist");
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.Unicode))
            {
                writer.Write(username+"%"+password);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                return result;
            }
        }
        public static string Login(string username ,string password)
        {
            try
            {
                string strURL = string.Format("{0}?snake={1}", url, "login");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST";
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(username + "%" + password);
                    writer.Flush();
                    writer.Close();
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string result = reader.ReadToEnd();
                    reader.Close();
                    return result;
                }
            }
            catch
            {
                return "服务器未启动！";
            }
        }

        public static string UpLoadRanking(int barid, int userid, int score, int len)
        {
            string strURL = string.Format("{0}?snake={1}", url, "uploadranking");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(barid + "%" + userid + "%" + score + "%" + len);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                return result;
            }
        }
        public static int GetUserID(string username)
        {
            string strURL = string.Format("{0}?snake={1}", url, "getuserid");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(username);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                return int.Parse(result);
            }
        }
        public static List<string> GetRanking(int bid)
        {
            string strURL = string.Format("{0}?snake={1}", url, "getranking");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";    
            using(StreamWriter writer=new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(bid);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
                reader.Close();
            }
            var vs = result.Split('%');
            List<string> ranking = new List<string>();
            foreach(var v in vs)
            {
                ranking.Add(v);
            }
            return ranking;
        }
        public static List<string> GetBarrierList()
        {
            string strURL = string.Format("{0}?snake={1}", url, "getbarrierlist");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
                reader.Close();
            }
            var vs = result.Split('%');
            List<string> barriers = new List<string>();
            foreach(var p in vs)
            {
                barriers.Add(p);
            }
            barriers.Remove(barriers.Last());
            return barriers;
        }
        public static string GetBarrierInfo(int bid)
        {
            string strURL = string.Format("{0}?snake={1}", url, "getbarrierinfo");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer=new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(bid);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result;
            using (StreamReader reader=new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
                reader.Close();
            }
            return result;
        }
        public static string GetUsername(int id)
        {
            string strURL = string.Format("{0}?snake={1}", url, "getusername");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(id);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
                reader.Close();
            }
            return result;
        }
    }
}
